package com.qipay.sp.excutor.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.qipay.baas.model.AlipayTransferLog;
import com.qipay.baas.model.Order;
import com.qipay.mapper.AlipayTransferLogMapper;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class AlipayTransferExcutor extends AbstractExcutor {

    private final static String CONFIG_APPID_NAME="appid";
    private final static String CONFIG_ALIPAY_PUBLIC_NAME="rsa_public_key_alipay";
    private final static String CONFIG_RSA_PRIVATE_NAME="rsa_private_key";
    private final static String API_URL="https://openapi.alipay.com/gateway.do";

    @Autowired
    AlipayTransferLogMapper alipayTransferLogMapper;

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_WAP;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        return extendOrderParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        AlipayTransferLog alipayTransferLog = (AlipayTransferLog) params.get("alipayTransferLog");
        Object appId = getVal(spconfig, AlipayTransferExcutor.CONFIG_APPID_NAME);
        Object rsaPrivateKey = getVal(spconfig, AlipayTransferExcutor.CONFIG_RSA_PRIVATE_NAME);
        Object alipayPublicKey = getVal(spconfig, AlipayTransferExcutor.CONFIG_ALIPAY_PUBLIC_NAME);

        String outBizNo = alipayTransferLog.getTransferNo();
        String payeeAccount = alipayTransferLog.getCollectionAlipay();
        String amount = new BigDecimal(alipayTransferLog.getAmount()).divide(new BigDecimal(100)).toString();
        String remark = "单笔转账金额为："+amount;

        AlipayClient alipayClient = new DefaultAlipayClient(AlipayTransferExcutor.API_URL, appId.toString(), rsaPrivateKey.toString(), "json", "UTF-8", alipayPublicKey.toString(), "RSA2"); // 获得初始化的AlipayClient
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizContent("{" +
                "\"out_biz_no\":\""+outBizNo+"\"," +
                "\"payee_type\":\"ALIPAY_LOGONID\"," +
                "\"payee_account\":\""+payeeAccount+"\"," +
                "\"amount\":\""+amount+"\"," +
                "\"remark\":\""+remark+"\"" +
                "}");
        AlipayFundTransToaccountTransferResponse response = null;
        Map<String, Object> resultMap = new HashMap<>();
        try {
            response = alipayClient.execute(request);
        }catch (Exception e){
            resultMap.put("result_code", "1");
            resultMap.put("message", "支付宝单笔转账接口请求失败");
            resultMap.put("alipayTransferLog",alipayTransferLog);
        }
        if(response!=null){
            //保留响应参数
            alipayTransferLog.setRemark(response.getBody());

            if(response.isSuccess()){
                resultMap.put("result_code", "0");
                alipayTransferLog.setStatus((byte)1);
            } else {
                logger.info("支付宝单笔转账接口请求失败："+response.getMsg());
                resultMap.put("result_code", "1");
                resultMap.put("message", "支付宝单笔转账接口请求失败");
                resultMap.put("alipayTransferLog",alipayTransferLog);
            }
            alipayTransferLogMapper.updateByPrimaryKey(alipayTransferLog);
        }
        return resultMap;
    }
}
